package pers.mihao.quickstream.publish.strategy;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import pers.mihao.quickstream.common.util.CollectionUtil;
import pers.mihao.quickstream.publish.strategy.context.ChooseContext;
import pers.mihao.quickstream.publish.strategy.directory.AccountDirectory;
import pers.mihao.quickstream.publish.strategy.dto.ChooseAccount;
import pers.mihao.quickstream.publish.strategy.dto.ChooseAccountResult;
import pers.mihao.quickstream.publish.strategy.dto.ChooseAccountScore;
import pers.mihao.quickstream.publish.strategy.filter.AccountFilter;
import pers.mihao.quickstream.publish.strategy.scorer.AccountScorer;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author mihao
 * @since 2024/11/21
 */
@Component
@Slf4j
public class AccountChooseStrategy {

    @Resource(name = "accountDirectory")
    AccountDirectory accountDirectory;

    @Resource(name = "accountScorer")
    AccountScorer accountScorer;

    @Resource(name = "accountFilter")
    AccountFilter accountFilter;

    /**
     * 1.选出所有账户，并过滤。（过滤出来都是可以发布的）
     *     1.1 状态过滤
     *     1.2 检查之前是否发过某个素材
     * 2. 评分
     *     2.1 根据黑白名单进行打分
     *     2.2 根据视频标签匹配进行打分
     *     2.3 根据视频质量和账户对比
     * 3. 选择账户过滤器
     *     3.1 一个平台只选一个账户过滤
     */
    public ChooseAccountResult chooseAccount(ChooseContext chooseContext) {
        // 选出账户
        List<ChooseAccount> accounts = accountDirectory.list(chooseContext);
        if (CollectionUtil.isEmpty(accounts)) {
            return new ChooseAccountResult();
        }

        // 评分
        List<ChooseAccountScore> scores = accounts.stream()
                .map(a -> ChooseAccountScore.builder().chooseAccount(a)
                        .scope(accountScorer.scoring(a, chooseContext)).build())
                .collect(Collectors.toList());


        // 最终过滤
        accounts = accountFilter.accountFilter(chooseContext, scores);
        return ChooseAccountResult.builder().accounts(accounts).build();
    }


}
